package com.sdkj.dspservice.modules.demand.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.dspservice.common.base.entity.BaseResult;
import com.sdkj.dspservice.common.constant.HttpStatus;
import com.sdkj.dspservice.common.react.AntdProResult;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.demand.entity.DemandType;
import com.sdkj.dspservice.modules.demand.service.DemandTypeService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求类型表Controller
 *
 * @author tm
 */
@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/demand/demandType")
public class DemandTypeController {

    @Autowired
    private DemandTypeService demandTypeService;

    @RequiresPermissions("demand:demandType:view")
    @RequestMapping(value = "get")
    public AntdProResult get(String id, HttpServletResponse response) {
        AntdProResult result = new AntdProResult();
        if (StringUtils.isNotBlank(id)) {
            DemandType demandType = demandTypeService.getById(id);
            result.setData(demandType);
        } else {
            result.error(HttpStatus.STATUS_400,
                    "id不能为空");
        }
        return result;
    }

    @RequiresPermissions("demand:demandType:view")
    @RequestMapping(value = "list")
    public AntdProResult list(DemandType demandType) {
        AntdProResult result = new AntdProResult();
        Page<DemandType> demandTypePage = demandTypeService.page(new Page<>(demandType.current, demandType.pageSize), new QueryWrapper<>(demandType));
        result.success(demandTypePage);
        return result;
    }

    @RequiresPermissions("demand:demandType:edit")
    @RequestMapping(value = "save", produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseResult save(@Valid @RequestBody DemandType demandType) {
        BaseResult result = new BaseResult();
        demandTypeService.saveOrUpdate(demandType);
        return result;
    }

    @RequiresPermissions("demand:demandType:edit")
    @RequestMapping(value = "remove")
    public BaseResult delete(String id, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if (StringUtils.isNotBlank(id)) {
            demandTypeService.removeById(id);
        } else {
            response.setStatus(HttpStatus.STATUS_400);
            result.setErrorMessage("id不能为空");
        }
        return result;
    }

    /**
     * 获取数据源select列表
     *
     * @param demandType
     * @return
     */
    @ApiOperation(value = "获取select列表")
    @GetMapping(value = "listForSelect")
    public AntdProResult listForSelect(DemandType demandType) {
        AntdProResult result = new AntdProResult();
        List<DemandType> demandTypeList = demandTypeService.list(demandType);
        // 转换为{label:xxx,value:xxx}
        List<Map<String, Object>> mapList = new ArrayList<>();
        for(DemandType r : demandTypeList){
            Map<String, Object> map = new HashMap<>(2);
            map.put("label", r.getName());
            map.put("value", r.getId());
            mapList.add(map);
        }
        result.success(mapList);
        return result;
    }

}